package cn.szzsi.common.model;

import cn.szzsi.common.model.base.BaseProductEvaluate;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by yuanzheng on 2016/12/15.
 */
//@TableBind(tableName = "b_product_evaluate",pkName = "id")
public class ProductEvaluate extends BaseProductEvaluate<ProductEvaluate> {
    public static final ProductEvaluate dao = new ProductEvaluate().dao();

    public static final void saveEvaluate(String productId, String content, String imageStore,Integer countStar, String memberId, String uid){
        ProductEvaluate productEvaluate=new ProductEvaluate();
        productEvaluate.set("productId",productId);
        productEvaluate.set("content",content);
        productEvaluate.set("imageStore",imageStore);
        productEvaluate.set("countStar",countStar);
        productEvaluate.set("memberId",memberId);
        productEvaluate.set("uid",uid);
        productEvaluate.set("createDate",new Date());
        productEvaluate.save();

    }

    /**
     * 根据商品id查询所有评价
     * @param productId
     * @return
     */
    public List<ProductEvaluate> getEvaluateList(String productId){
        return dao.find("select * from mall_product_evaluate where productId=? order by createDate desc",productId);
    }

    /**
     * zhengy
     * 根据商品查询商品所有评论
     * @param product
     * @return
     */
    public List<ProductEvaluate> getEvaluateList(Product product){
        //1.查询同一系列的所有商品
        List <Product> allProducts= Product.dao.find("select id from mall_product where specificationtype=?",product.getSpecificationtype());
        StringBuffer productIds=new StringBuffer();
        for(Product product1:allProducts){
            productIds.append(product1.getId()+",");
        }
        String sql="select * from mall_product_evaluate where find_in_set(productId,'"+productIds+"') order by createDate desc";
        return dao.find(sql);

    }

    public List<String> getEvaluateImageStore(){
        String imageStore=getStr("imageStore");
        String imageArray[]=imageStore.split(",");
        List<String> list = Arrays.asList(imageArray);
        return list;
    }

    public Member getMember(){
        return Member.dao.findById(getStr("memberId"));
    }
;}
